﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {

        /// <summary>
        /// Adds the new place to the geographical places database.
        /// </summary>
        /// <param name="title">The title of the new place.</param>
        /// <param name="latitude">The latitude. -90 to 90.</param>
        /// <param name="longtitude">The longtitude. -180 to 180.</param>
        /// <param name="type">The type of the new place. Use places.getTypes to see all of them.</param>
        /// <param name="country">The country ID. Use places.getCountries to see all of them. (unnecessary)</param>
        /// <param name="city">The city ID. Use places.getCities to see all of them. (unnecessary)</param>
        /// <param name="address">The address of the new place. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Place ID if success.</returns>
        public long Places_Add(string title, int latitude, int longtitude, int type, int? country, long? city, string address, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("title", title));
            list.Add(new Parameter("latitude", latitude.ToString()));
            list.Add(new Parameter("longtitude", longtitude.ToString()));
            list.Add(new Parameter("type", type.ToString()));
            if (country != null) list.Add(new Parameter("country", country.ToString()));
            if (city != null) list.Add(new Parameter("city", city.ToString()));
            if (address != null) list.Add(new Parameter("address", address));
            return this.SendRequest("places.add", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the palceslist by their IDs.
        /// </summary>
        /// <param name="places">The places IDs. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Places.</returns>
        public long Places_GetById(long?[] places, ApiCallback<Place[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (places.Length > 0)
            {
                string placesString = "";
                foreach (int s in places)
                    placesString += s.ToString() + ",";
                placesString = placesString.Remove(placesString.Length - 1);
                list.Add(new Parameter("places", placesString)); 
            }        
            return this.SendRequest("places.getById", callback, list.ToArray());
        }


        /// <summary>
        /// Searches the places.
        /// </summary>
        /// <param name="latitude">The latitude in which radius should search.</param>
        /// <param name="longtitude">The longtitude in which radius should search..</param>
        /// <param name="q">The query string. (unnecessary)</param>
        /// <param name="radius">The radius type. From 1 to 4. 1 - 100 meters, 2 - 800 meters, 3 - 6 kilometers, 4 - 50 kilometers. 1 by default.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Searched places.</returns>
        public long Places_Search( int latitude, int longtitude, string q, int? radius, ApiCallback<SearchedPlace[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("latitude", latitude.ToString()));
            list.Add(new Parameter("latitude", latitude.ToString()));
            if (q.Length > 0) list.Add(new Parameter("q", q));
            if (radius != null) list.Add(new Parameter("radius", radius.ToString()));
            return this.SendRequest("places.search", callback, list.ToArray());
        }

        /// <summary>
        /// Marks the user on specific place.
        /// </summary>
        /// <param name="place_id">The place ID.</param>
        /// <param name="text">The comment for this place. </param>
        /// <param name="latitude">The latitude of mark. (unnecessary)</param>
        /// <param name="longtitude">The longtitude of mark. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New comment ID if success.</returns>
        public long Places_Checkin(long place_id, string text, int? latitude, int? longtitude, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("place_id", place_id.ToString()));
            if (text.Length > 0) list.Add(new Parameter("text", text));
            if (latitude != null) list.Add(new Parameter("latitude", latitude.ToString()));
            if (longtitude != null) list.Add(new Parameter("longtitude", longtitude.ToString()));
            return this.SendRequest("places.checkin", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the list of places with specified parameters. Demo version.
        /// </summary>
        /// <param name="latitude">The latitude. (unnecessary)</param>
        /// <param name="longtitude">The longtitude. (unnecessary)</param>
        /// <param name="place">The place ID. (unnecessary)</param>
        /// <param name="uid">The user ID. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="timestamp">The timestamp to compare with. (unnecessary)</param>
        /// <param name="friends_only">The flag for showing only friends places. (unnecessary)</param>
        /// <param name="need_places">The flag for place inclusion. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Count of results and results array.</returns>
        public long Places_GetCheckin(int? latitude, int? longtitude,int? place, long? uid, int? offset, int? count, string timestamp, int? friends_only, int? need_places, ApiCallback<Object> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (latitude != null) list.Add(new Parameter("latitude", latitude.ToString()));
            if (longtitude != null) list.Add(new Parameter("longtitude", longtitude.ToString()));
            if (place != null) list.Add(new Parameter("place", place.ToString()));
            if (uid != null) list.Add(new Parameter("uid", uid.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (timestamp.Length > 0) list.Add(new Parameter("timestamp", timestamp));
            if (friends_only != null) list.Add(new Parameter("friends_only", friends_only.ToString()));
            if (need_places != null) list.Add(new Parameter("need_places", need_places.ToString()));            
            return this.SendRequest("places.getCheckins", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the list of all possible places.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Places Types</returns>
        public long Places_GetTypes(ApiCallback<PlaceType[]> callback)
        {
            return this.SendRequest("places.getTypes", callback, null);
        }

        /// <summary>
        /// Gets the list of all possible countries.
        /// </summary>
        /// <param name="need_full">The flag for all countries list. (unnecessary)</param>
        /// <param name="code">The code of countries. Ex.: RU,UK. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Places_GetCountries(int? need_full, string code, ApiCallback<CountryType[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (need_full != null) list.Add(new Parameter("need_full", need_full.ToString()));
            if (code != null) list.Add(new Parameter("code", code));
            return this.SendRequest("places.getCountries", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the list of cities.
        /// </summary>
        /// <param name="country">The country ID. Got by places.getCountries</param>
        /// <param name="q">The query for search.(unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Places_GetCities(long country, string q, ApiCallback<CityType[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("country", country.ToString()));
            if (q != null) list.Add(new Parameter("q", q));
            return this.SendRequest("places.getCities", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the country info by IDs.
        /// </summary>
        /// <param name="cid">The country IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Searched Countries.</returns>
        public long Places_GetCountryById(long[] cid, ApiCallback<SearchedCountry[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            string cidString = "";
            foreach (int s in cid)
                cidString += s.ToString() + ",";
            cidString = cidString.Remove(cidString.Length - 1);
            list.Add(new Parameter("cid", cidString));
      
            return this.SendRequest("places.getCountryById", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the city info by IDs.
        /// </summary>
        /// <param name="cid">The city IDs.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Searched Cities.</returns>
        public long Places_GetCityById(long[] cid, ApiCallback<SearchedCity[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            string cidString = "";
            foreach (int s in cid)
                cidString += s.ToString() + ",";
            cidString = cidString.Remove(cidString.Length - 1);
            list.Add(new Parameter("cid", cidString));            
            return this.SendRequest("places.getCityById", callback, list.ToArray());
        }

    }
}
